<?php

namespace App\Jobs;

use App\Models\ImQueueErrorLogs;
use App\Services\Huanxin\UserServices;
use App\Services\RongCloud\UserService as RongUserService;
use App\Services\Netease\UserServices as NeteaseUserService;
use App\Services\Aliyun\Video\VideoServices;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class RegisterTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data = null;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data = null)
    {
        //
        $this->data = $data;
    }

    /**
     * @desc 队列自动审核朋友圈的图片
     * @return bool
     * @author zhouzhou
     * @time 2021/12/20 18:34
     */
    public function handle()
    {
        // redis的数据
        $data = $this->data;
        if (!empty($data))
        {
            $userId = $data['user_id'];
            try {
                // 环信
                $UserServices = new UserServices();
                $UserServices->createUser($userId);

                // 融云
                $userRongServices = new RongUserService();
                $userRongServices->register($userId, 'im' . $userId);

                // 网易云信
                $userNeteaseServices = new NeteaseUserService();
                $userNeteaseServices->createUser($userId, 'im' . $userId);
            } catch (\Exception $e)
            {
                $imQueueErrorLogs = new ImQueueErrorLogs();
                $imQueueErrorLogs->insertQueueError(json_encode(['data'=>$data]), 1, $e->getMessage());
            }
        }

    }
}
